package com.yyr.callmessage.core.util;

import com.google.common.base.Strings;
import com.yyr.callmessage.core.config.LocaleConfig;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

public class LangUtil {

    /**
     * 传入三个国家语言，根据用户请求的语言返回内容，没有则返回默认内容
     * @param string
     * @param ch_string
     * @param en_string
     * @return
     */
    public static String getStringByLang(String string, String ch_string, String en_string) {
        Locale locale = LocaleContextHolder.getLocale();
        if (locale.getLanguage().equalsIgnoreCase(Locale.CHINESE.getLanguage()) && !Strings.isNullOrEmpty(ch_string)) {
            return ch_string;
        }

        if (locale.getLanguage().equalsIgnoreCase(Locale.ENGLISH.getLanguage()) && !Strings.isNullOrEmpty(en_string)) {
            return en_string;
        }

        return string;
    }

    /**
     * 根据用户请求的语言从本地化文件找
     * @param string
     * @return
     */
    public static String getStringByLang(String string) {
        return LocaleConfig.getMessageInstance().getMessage(string, null, "", LocaleContextHolder.getLocale());
    }
}
